<?php

namespace api\modules\v1\controllers\common;

use Yii;
use common\helpers\CommonHelper;
use common\models\common\Provinces;
use api\controllers\OnAuthController;

/**
 * Class ProvincesController
 * @package api\modules\v1\controllers\common
 */
class ProvincesController extends OnAuthController
{
    /**
     * @var Provinces
     */
    public $modelClass = Provinces::class;

    /**
     * @api {get} /v1/common/provinces/list 城市列表
     * @apiName list
     * @apiGroup Provinces
     *
     * @apiParam {String} keywords                                         关键词【城市名称or拼音】
     *
     * @apiSuccess {Object}  data                                          返回数据
     * @apiSuccess {Array}   data.list                                     列表
     * @apiSuccess {Number}  data.list.id                                  城市ID
     * @apiSuccess {String}  data.list.name                                名称
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['keywords'] = $params['keywords'] ?? '';
        if (empty($params['keywords'])) {
            return [];
        }

        // 直辖市
        $cityIds = [
            Provinces::CITY_BEIJING,
            Provinces::CITY_TIANJIN,
            Provinces::CITY_SHANGHAI,
            Provinces::CITY_CHONGQING,
            Provinces::CITY_HK,
            Provinces::CITY_MO
        ];

        // 查询结果
        $data = Yii::$app->services->provinces->findList([
            'select' => ['id', 'title AS name'],
            'other' => [
                'or',
                ['like', 'title', $params['keywords']],
                ['like', 'jianpin', $params['keywords']],
                ['like', 'pinyin', $params['keywords']],
            ],
            /*'other' => [
                'and',
                [
                    'or',
                    ['like', 'title', $params['keywords']],
                    ['like', 'jianpin', $params['keywords']],
                    ['like', 'pinyin', $params['keywords']],
                ],
                [
                    'or',
                    ['level' => [Provinces::LEVEL_CITY, Provinces::LEVEL_AREA]],
                    ['id' => $cityIds],
                ],

            ],*/
        ]);
        return [
            'list' => $data,
        ];
    }
}
